<script setup>
import { computed, ref } from "vue";
import BranchNode from "./base/BranchNode.vue";
import nodeMixin from "../NodeMixin";
import { DIC_PROP, filterDictLabel } from "@/flow/support/dict-prop";

const props = defineProps({
  ...nodeMixin.props,
  moveLn: Boolean,
  moveRn: Boolean,
  isDefault: Boolean
});
const emit = defineEmits(["insertNode", "delete", "select", "moveL", "moveR"]);
const _value = defineModel();

const showErr = ref(false);
const errInfo = ref(null);

defineExpose({ validate });

const content = computed(() => {
  if (props.isDefault) {
    return "其他情况将进入此分支";
  } else {
    let desc = "请设置条件";
    const prop = _value.value.props;
    if (prop.groups.length > 0 && prop.groups[0].conditions.length > 0) {
      desc = prop.groups
        .map(group => {
          if ((group.conditions || []).length === 0) {
            return "请添加条件";
          }
          const hasMore = prop.groups.length > 1 && group.conditions.length;
          return (
            (hasMore ? "[" : "") +
            group.conditions
              .map(cd => {
                if (prop.conditionType === "0") {
                  if (cd.varKeyVal && cd.operator && cd.varVal) {
                    let opDesc = filterDictLabel(
                      DIC_PROP.OPERATOR,
                      cd.operator
                    );
                    if (!opDesc) {
                      opDesc = cd.operator;
                    }
                    return "表单字段" + cd.varKeyVal + opDesc + cd.varVal;
                  }
                } else {
                  if (cd.varKeyVal) {
                    return cd.varKeyVal;
                  }
                }
                return "未配置条件";
              })
              .join(` ${group.groupItemType ? "且" : "或"} `) +
            (hasMore ? "]" : "")
          );
        })
        .join(` ${prop.groupType ? "且" : "或"} `);
    }
    return desc;
  }
});

function validate(errs) {
  //校验非默认节点
  showErr.value = true;
  if (!props.isDefault) {
    const prop = _value.value.props;
    if (prop.groups.length === 0) {
      errInfo.value = "请添加条件组";
      errs.push(`条件 ${_value.value.name}未添加条件组`);
      return;
    } else {
      for (let i = 0; i < prop.groups.length; i++) {
        const group = prop.groups[i];
        if (group.conditions.length === 0) {
          errInfo.value = "请给条件组设置条件";
          errs.push(`${_value.value.name}-${group.name}未添加条件`);
          return;
        }
        for (const cd of group.conditions) {
          if (!cd.varKeyVal || !cd.operator || !cd.varVal) {
            errInfo.value = `请完善条件项: ${(cd.varKeyVal || []).join("-")}`;
            errs.push(`${_value.value.name}-条件组${i + 1}内条件未完善`);
            return;
          }
        }
      }
    }
  }
  showErr.value = false;
}
</script>

<template>
  <BranchNode
    v-model="_value"
    :readonly="props.readonly"
    :show-error="showErr"
    :error-info="errInfo"
    :index="index"
    color="#59B9A4"
    header-icon="share"
    :is-default="props.isDefault"
    :content="content"
    :moveRn="props.moveRn"
    :moveLn="props.moveLn"
    :desc="`优先级 ${props.index + 1}`"
    :flow-inst-id="props.flowInstId"
    :flowHistory="flowHistory"
    @insertNode="type => emit('insertNode', type)"
    @move-l="emit('moveL')"
    @move-r="emit('moveR')"
    @delete="emit('delete')"
    @select="emit('select', { node: _value.value })"
  />
</template>

<style scoped></style>
